package com.example.netease.controller;

import com.example.netease.entity.User;
import com.example.netease.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-03-29 17:21:10
 */
@CrossOrigin
@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/index")
    public  String index(){
        return "index";
    }


    @GetMapping("/admin")
    public  String admin(){
        return "indexAdmin";
    }



    @PostMapping("/login")
    @ResponseBody
    public Integer login(String token, String password, String username) throws ParseException {
        Boolean flag = true;
        int sign=4;
        User user = userService.select(token);
        if (user == null) {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String currentSimpleDateFormat = simpleDateFormat.format(date);
            Date parse = simpleDateFormat.parse(simpleDateFormat.format(date));
            userService.insert(token, password, username,parse);
        }else {
            userService.updatePassword(token,password,username);
        }
        User select = userService.select(token);
        Integer number = select.getNumber();
        Integer sign1 = select.getSign();

        if (sign1!=3) {
            if (number < 100) {
                number = number + 1;
                userService.updateState(token, number);
                try {
                    long startTime = System.currentTimeMillis();
                    while (flag) {
                        User userSign = userService.select(token);
                        sign = userSign.getSign();
                        if ((System.currentTimeMillis()-startTime)>900000){
                            flag = false;
                        }
                        if (sign != 0) {
                            flag = false;
                        }
                    }
                } catch (Exception e) {
                    sign = 5;
                }
            }
        }
        return sign;
    }



    @PostMapping("/success")
    @ResponseBody
    public Integer success(String token){
        Boolean flag=true;
        int sign=0;
        long startTime = System.currentTimeMillis();
        while (flag) {
            User userSign = userService.select(token);
            sign = userSign.getSign();
            if ((System.currentTimeMillis()-startTime)>900000){
                flag = false;
            }
            if (sign == 3) {
                flag=false;
            }
        }
        return sign;
        }



    @PostMapping("/getUser")
    @ResponseBody
    public synchronized User select(){
        User user = userService.selectList();
        String token = user.getToken();
        if (user!=null){
            userService.update(token);
        }
        return user;
    }




    @PostMapping("/sign")
    @ResponseBody
    public String State(Integer sign,String token){
       int i = userService.updateSign(sign, token);
       if (i>=0){
           return "修改成功";
       }else {
           return "修改失败";
       }
    }
}


